# 列表推导式

"""
列表推导式：最终得到的是一个列表：
格式：
    lst = [i for i in range(100)]
    lst = [i for i in range(100) if 条件]
    lst = [i if 条件1 else 条件2 for i in range(100) ]

"""

# 传统
lst = []
for i in range(20):
    lst.append(i)
print(lst)

# 列表推导式
lst = [i for i in range(20)]
print(lst)

lst = [i + 2 for i in range(20)]  # 格式一
print(lst)

lst = [i for i in range(0, 101, 2)]  # 格式一
print(lst)

lst = [i for i in range(101) if i % 2 == 0]  # 格式二：带判断条件
print(lst)

lst2 = ['62', 'hello', '100', 'world', 'luck', '88']
lst = [word for word in lst2 if word.isalpha()]  # 全部是字母组成的新单词
print(lst)

# 如果是h开头的首字母大写，如果不是h开头的则全部转成大写  if else

lst = [word.title() if (word.isalpha() and word.startswith('h'))
       else word.upper()
       for word in lst2]
print(lst)

# 2层for 循环

lst3 = [(i, j) for i in range(5) for j in range(5)]  # 2层for循环
print(lst3)

# 实现将1-100 中的数字3个一组如[[1,2,3],[4,5,6]...]
lst = [[i, i + 1, i + 2] if i < 100 else [i] for i in range(1, 101, 3)]
print(lst)

nei_lst = [i for i in range(1, 101)]
lst = [nei_lst[i:i + 3] for i in range(0, len(nei_lst), 3)]
print(lst)
